from message.api import MessageService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import smtplib
from email.mime.text import MIMEText
from email.header import Header
class MessageServiceHandler:
    def sendMessage(self, to, message, type):
        if type ==  "mobile":
            print("sendMobleMsg")
        if type ==  "email":
            print("sendEmailMsg")
            messageObject = MIMEText(message,"plain","utf-8")
            messageObject['from']="waldo@blackmirror.top"
            messageObject['to'] =   to
            messageObject['Subject']= Header('系统消息','utf-8')
            try:
                smtpObject = smtplib.SMTP('smtp.exmail.qq.com')
                smtpObject.login("waldo@blackmirror.top","fXwbBNAE8W5639mS")
                smtpObject.sendmail("waldo@blackmirror.top", [to],messageObject.as_string())
                print("send message success")
            except smtplib.SMTPException:
                print("send message error")
        return True
if  __name__ == '__main__':
    hander = MessageServiceHandler()
    procssor =MessageService.Processor(hander)
    transport = TSocket.TServerSocket("127.0.0.1","9001")
    tfactory = TTransport.TFramedTransportFactory()
    pfactory =TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
    server = TServer.TSimpleServer(procssor,transport,tfactory,pfactory)
    print("python  thrift server start")
    server.serve()
    print("python server exit")


